"""
Case Type   : 系统内部使用工具
Case Name   : switchover完成后在新的备机执行build
Create At   : 2021/01/21
Owner       : opentestcase015
Description :
    1.在备机做switchover操作
    2.执行refrashconf进行信息写入
    3.重启集群
    4.查看集群状态，主备是否切换成功
    5.在新的备节点执行build
Expect      :
    1.在备机做switchover操作成功
    2.执行refrashconf进行信息写入成功
    3.重启集群成功
    4.查看集群状态，主备切换成功
    5.在新的备节点执行build成功
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class SystemInternalTools(unittest.TestCase):
    def setUp(self):
        LOG.info('--------------this is setup----------------')
        LOG.info('---Opengauss_Function_Tools_gs_ctl_Case0085开始执行-----')
        self.constant = Constant()
        self.PrimaryNode = Node('PrimaryDbUser')
        self.sh_primary = CommonSH('PrimaryDbUser')

    def test_system_internal_tools(self):
        LOG.info('-----------若为单机环境，后续不执行，直接通过----------')
        query_cmd = f'''source {macro.DB_ENV_PATH};
            gs_om -t status --detail;
            '''
        LOG.info(query_cmd)
        query_msg = self.PrimaryNode.sh(query_cmd).result()
        LOG.info(query_msg)
        if 'Standby' not in query_msg:
            return '单机环境，后续不执行，直接通过'
        else:
            self.StandbyNode = Node('Standby1DbUser')

        LOG.info('-----------------执行switchover---------------------')
        excute_cmd = f'''source {macro.DB_ENV_PATH};
            gs_ctl switchover -D {macro.DB_INSTANCE_PATH} -m fast ;
            '''
        LOG.info(excute_cmd)
        msg = self.StandbyNode.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.SWITCHOVER_SUCCESS_MSG, msg)

        LOG.info('-----------------进行refreshconf---------------------')
        excute_cmd = f'''source {macro.DB_ENV_PATH};
            gs_om -t refreshconf;
            '''
        LOG.info(excute_cmd)
        msg = self.PrimaryNode.sh(excute_cmd).result()
        LOG.info(msg)
        self.assertIn(self.constant.REFRESHCONF_SUCCESS_MSG, msg)

        LOG.info('---------------------重启数据库--------------------')
        self.sh_primary.restart_db_cluster()
        status = self.sh_primary.get_db_cluster_status()
        self.assertTrue("Normal" in status or 'Degraded' in status)

        LOG.info('-----------------查看主备状态---------------------')
        status_cmd = f'''source {macro.DB_ENV_PATH};
            gs_om -t status --detail;
            '''
        LOG.info(status_cmd)
        status_msg = self.PrimaryNode.sh(status_cmd).result()
        LOG.info(status_msg)
        self.node_msg = status_msg.splitlines()[10].strip()
        self.assertIn('Standby', self.node_msg)

        LOG.info('--------------备机重建--------------')
        rebuild_cmd = f'''source {macro.DB_ENV_PATH};
            gs_ctl build -D {macro.DB_INSTANCE_PATH};
            '''
        LOG.info(rebuild_cmd)
        rebuild_msg = self.PrimaryNode.sh(rebuild_cmd).result()
        LOG.info(rebuild_msg)
        self.assertIn(self.constant.BUILD_SUCCESS_MSG, rebuild_msg)

    def tearDown(self):
        LOG.info('--------------this is tearDown--------------')
        status_cmd = f''' source {macro.DB_ENV_PATH};
            gs_om -t status --detail;
            '''
        LOG.info(status_cmd)
        status_msg = self.PrimaryNode.sh(status_cmd).result()
        LOG.info(status_msg)
        self.node_msg = status_msg.splitlines()[10].strip()
        LOG.info(self.node_msg)
        if 'Standby' in self.node_msg:
            LOG.info('--------------恢复主备状态--------------')
            recover_cmd = f'''source {macro.DB_ENV_PATH};
                gs_ctl switchover -D {macro.DB_INSTANCE_PATH} -m fast;
                gs_om -t refreshconf;
                '''
            LOG.info(recover_cmd)
            recover_msg = self.PrimaryNode.sh(recover_cmd).result()
            LOG.info(recover_msg)
        else:
            return '主备节点正常'
        LOG.info('---Opengauss_Function_Tools_gs_ctl_Case0085执行完成----')
